import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Language } from "../../../../../extend/language/Language";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { PetExploreBattleListPanel } from "./PetExploreBattleListPanel";
import { PetExplorePetItem } from "./PetExplorePetItem";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { TweenMax } from "../../../../../com/greensock/TweenMax";
import { Cache } from "../../../cache/Cache";
import { PetUtil } from "../info/PetUtil";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { MsgManager } from "../../../manager/MsgManager";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GButton } from "../../../../../com/mui/controls/GButton";
//class PetExploreSelectPanel
    
    export  class PetExploreSelectPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._battlePetItemArray = [];
            this._packPetItemArray = [];
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=null;
            var loc2=null;
            ObjCreate.createBg(0, 0, 325, 265, this, ImagesConst.WindowCenterA);
            ObjCreate.createBg(5, 5, 315, 105, this);
            ObjCreate.createBg(5, 115, 315, 115, this);
            this._battlePetLabel = new Object /* flash.display.Bitmap */();
            this._battlePetLabel.x = 20;
            this._battlePetLabel.y = 15;
            this.addChild(this._battlePetLabel);
            ObjCreate.createTextField(Language.getString(54477), 110, 20, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._packPetLabel = new Object /* flash.display.Bitmap */();
            this._packPetLabel.x = 20;
            this._packPetLabel.y = 120;
            this.addChild(this._packPetLabel);
            ObjCreate.createTextField(Language.getString(54508), 110, 125, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._battleListPanel = new PetExploreBattleListPanel();
            this._battleListPanel.x = 20;
            this._battleListPanel.y = 48;
            this.addChild(this._battleListPanel);
            this._battleListPanel.clickFun = this.onBattlePetItemClickHandler;
            var loc3=0;
            while (loc3 < 5) 
            {
                loc1 = new PetExplorePetItem();
                loc1.setSize(38, 38);
                ObjCreate.createBitmap(ImagesConst.PetCircleBg, 20 + loc3 * 60, 145, this);
                loc1.x = 25 + loc3 * 60;
                loc1.y = 150;
                loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onPackPetItemClickHandler);
                loc1.isDragAble = false;
                loc1.isDropAble = false;
                this._packPetItemArray.push(loc1);
                this.addChild(loc1);
                ++loc3;
            }
            ObjCreate.createTextField(Language.getString(28021), 20, 205, 300, 20, this, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._saveBtn = ObjCreate.createGButton(Language.getString(54510), 130, 235, 70, 24, this);
            this._saveBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSaveBtnClickHandler);
            this._tipTxt = ObjCreate.createTextField(Language.getString(54511), 0, 235, 325, 20, this, GlobalStyle.textFormat6);
            this._tipTxt.visible = false;
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._petSortBtn = ObjCreate.createGLoadedButton(ResFileConst.SelectPet, 325, 0, 25, 158, this);
            this._petSortBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onPetSortBtnClickHandler);
            this._showBtn = GlobalClass.getInstance("showBtn");
            this._showBtn.x = 338;
            this._showBtn.y = 9;
            this._showBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onPetSortBtnClickHandler);
            this.addChild(this._showBtn);
            LoaderHelp.addResCallBack(ResFileConst.petExplore, this.onBitmapLoaderHandler);
            return;
        }

        /* internal  */onBitmapLoaderHandler(): void
        {
            this._battlePetLabel.bitmapData = GlobalClass.getBitmapData(ImagesConst.BattlePetLabel);
            this._packPetLabel.bitmapData = GlobalClass.getBitmapData(ImagesConst.PackPetLabel);
            return;
        }

        public showPanel(arg1: Boolean): void
        {
            var loc1=this.x;
            var loc2=17;
            if (arg1) 
            {
                loc2 = 17;
                this._showBtn.scaleX = -1;
            }
            else 
            {
                loc2 = -310;
                this._showBtn.scaleX = 1;
            }
            TweenMax.to(this, 1, {"x": loc2});
            return;
        }

        /* internal  */onPetSortBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this.x;
            if (loc1 < 0) 
            {
                this.showPanel(true);
            }
            else 
            {
                this.showPanel(false);
            }
            return;
        }

        public updatePackPet(arg1: Boolean): void
        {
            var loc4=null;
            var loc5=null;
            var loc8=null;
            var loc9=null;
            var loc1=Cache.instance.pet.getPetsByJudge(PetUtil.isCanExplore);
            var loc2=Cache.instance.petExplore.petExploreInfo;
            var loc3=[];
            if (loc2) 
            {
                loc3 = loc2.petUids;
            }
            var loc6=[];
            var loc7=0;
            while (loc7 < loc3.length) 
            {
                loc5 = loc3[loc7] as string;
                if ((loc4 = Cache.instance.pet.getPetInfoByUid(loc5)) && PetUtil.isCanExplore(loc4)) 
                {
                    loc6.push(loc5);
                }
                ++loc7;
            }
            this._battleListPanel.updateByPetUids(loc6, arg1);
            var loc10=0;
            while (loc10 < this._packPetItemArray.length) 
            {
                loc8 = this._packPetItemArray[loc10] as PetExplorePetItem;
                loc9 = loc1[loc10];
                loc8.petInfo = loc9;
                if (loc9) 
                {
                    if (!arg1) 
                    {
                        if (Cache.instance.petExplore.isPetInBattleList(loc9.uid)) 
                        {
                            loc8.selected = true;
                        }
                        else 
                        {
                            loc8.selected = false;
                        }
                    }
                }
                ++loc10;
            }
            return;
        }

        public resetSaveButton(): void
        {
            var loc2=false;
            var loc1=Cache.instance.petExplore.petExploreInfo;
            if (loc1) 
            {
                loc2 = loc1.isInExplore;
            }
            this._saveBtn.visible = !loc2;
            this._tipTxt.visible = !this._saveBtn.visible;
            return;
        }

        /* internal  */onBattlePetItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=arg1.target as PetExplorePetItem;
            if (loc1.petInfo) 
            {
                loc2 = loc1.petInfo.uid;
                loc1.petInfo = null;
                if (loc3 = this.getPackPetItemByUid(loc2)) 
                {
                    loc3.selected = false;
                }
            }
            return;
        }

        /* internal  */onPackPetItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc1=arg1.target as PetExplorePetItem;
            if (loc1.petInfo && !loc1.selected) 
            {
                loc1.selected = true;
                loc2 = this._battleListPanel.getBlankBattlePetItem();
                if (loc2) 
                {
                    loc2.petInfo = loc1.petInfo;
                }
            }
            return;
        }

        /* internal  */getPackPetItemByUid(arg1: string): PetExplorePetItem
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._packPetItemArray.length) 
            {
                loc1 = this._packPetItemArray[loc2] as PetExplorePetItem;
                if (loc1.petInfo.uid == arg1) 
                {
                    return loc1;
                }
                ++loc2;
            }
            return null;
        }

        /* internal  */isPetInCanExploreList(arg1: string, arg2: Array<any>): Boolean
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < arg2.length) 
            {
                loc1 = arg2[loc2] as SPetInfo;
                if (arg1 == loc1.uid) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        public getBattlePetUids(): Array<any>
        {
            return this._battleListPanel.getBattlePetUids();
        }

        /* internal  */onSaveBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this.getBattlePetUids();
            if (loc1.length == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54512));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreSetOrder, {"uids": loc1, "isShowTip": true}));
            return;
        }

        /* internal */ /* var */_showBtn: Object /* flash.display.SimpleButton */;

        /* internal */ /* var */_petSortBtn: GLoadedButton;

        /* internal */ /* var */_saveBtn: GButton;

        /* internal */ /* var */_battleListPanel: PetExploreBattleListPanel;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_battlePetLabel: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_packPetLabel: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_battlePetItemArray: Array<any>;

        /* internal */ /* var */_packPetItemArray: Array<any>;
    }
